<template>
  <div>
    <el-table :data="tableData5" style="width: 100%; margin: 16px;" border row-key="id" :expand-row-keys="expandRowkeys">
      <el-table-column type="expand">
        <template slot-scope="props">
          <expand-child :parentData="props.row" :ref="'expand-' + props.row.id"></expand-child>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="props">
          <el-button type="text" @click="addChild(props.row.id)">上传</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="商品 ID"
        prop="id">
      </el-table-column>
      <el-table-column
        label="商品名称"
        prop="name">
      </el-table-column>
      <el-table-column
        label="描述"
        prop="desc">
      </el-table-column>
    </el-table>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="file" prop="file">
          <el-input v-model="form.file" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="date" prop="date">
          <el-input v-model="form.date" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="memo" prop="memo">
          <el-input v-model="form.memo" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitChild">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

<script>
  import expandChild from './table-expand-child'

  export default {
    data() {
      return {
        tableData5: [{
          id: '12987122',
          name: '好滋好味鸡蛋仔',
          category: '江浙小吃、小吃零食',
          desc: '荷兰优质淡奶，奶香浓而不腻',
          address: '上海市普陀区真北路',
          shop: '王小虎夫妻店',
          shopId: '10333'
        }, {
          id: '12987123',
          name: '好滋好味鸡蛋仔',
          category: '江浙小吃、小吃零食',
          desc: '荷兰优质淡奶，奶香浓而不腻',
          address: '上海市普陀区真北路',
          shop: '王小虎夫妻店',
          shopId: '10333'
        }, {
          id: '12987125',
          name: '好滋好味鸡蛋仔',
          category: '江浙小吃、小吃零食',
          desc: '荷兰优质淡奶，奶香浓而不腻',
          address: '上海市普陀区真北路',
          shop: '王小虎夫妻店',
          shopId: '10333'
        }, {
          id: '12987126',
          name: '好滋好味鸡蛋仔',
          category: '江浙小吃、小吃零食',
          desc: '荷兰优质淡奶，奶香浓而不腻',
          address: '上海市普陀区真北路',
          shop: '王小虎夫妻店',
          shopId: '10333'
        }],
        childData: {},
        dialogFormVisible: false,
        form: {
          id: '',
          file: '',
          date: '',
          memo: ''
        },
        expandRowkeys:[]
      }
    },
    components: {
      expandChild
    },
    methods: {
      addChild(id) {
        this.form.id = id;
        this.dialogFormVisible = true;
      },
      submitChild() {
        // form表单提交后台


        // 提交完成后刷新
        this.dialogFormVisible = false;

        this.expandRowkeys.push(this.form.id); // 控制展开用，需要展开页面渲染之后才能获取ref

        this.$nextTick(()=>{
          this.$refs['expand-' + this.form.id].refresh();
          this.$refs['form'].resetFields();
        });
      }
    }
  }
</script>
